package com.lgrousset.loanssimulator.client.chart.widget;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.lgrousset.loanssimulator.client.EventBus;
import com.lgrousset.loanssimulator.client.chart.event.LoanAmountChangedEvent;
import com.lgrousset.loanssimulator.client.chart.event.LoanChangedEvent;
import com.lgrousset.loanssimulator.shared.model.Loan;

public class LoanFields extends Composite {
	private HorizontalPanel panel = new HorizontalPanel();
	private LoanFieldAmount loanFieldAmount = new LoanFieldAmount();
	
	private Loan loan;
	
	public LoanFields(Loan loanp){
		this.loan = loanp;
		
		panel.setStyleName("loanFields");
		
		loanFieldAmount.setValue(loan.getAmount().toString());
		panel.add(loanFieldAmount);
		
		LoanAmountChangedEvent.register(EventBus.get(), new LoanAmountChangedEvent.Handler() {
			@Override
			public void onChange(LoanAmountChangedEvent p) {
				loan.setAmount(Double.valueOf(loanFieldAmount.getValue()));
				
				LoanChangedEvent customEvent = new LoanChangedEvent();                                        
                EventBus.get().fireEvent(customEvent);
			}
		});
		
		initWidget(panel);
	}
}
